<?php

/**
 * wyniki actions.
 *
 * @package    edmuch
 * @subpackage wyniki
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class wynikiActions extends sfActions
{
  
  public function preExecute()
  {
    $this->user = $this->getUser()->getGuardUser();
  }  
    
  public function executeIndex(sfWebRequest $request)
  {
    $this->user = $this->getUser()->getGuardUser();
    
    $userid = $this->getUser()->getGuardUser()->getId();
    
    $query = Doctrine_Query::create()
      ->from('GameResult a')
      ->select('a.*, DATEDIFF(NOW(), a.end_date) AS ile_dni, COUNT(a.score) as ilosc, MAX(a.score) as max_pkt, AVG(a.score) as avg_pkt')
      ->where('a.user_id = ?', $userid)
      ->groupBy('a.games_id')
      ->addGroupBy('a.rozgrywka_id')
      ->addOrderBy('a.end_date DESC');    
    
    $this->game_results = new sfDoctrinePager(
        'GameResults',
        sfConfig::get('app_max_games_result_on_page')
    );
    $this->game_results->setQuery($query);
    $this->game_results->setPage($request->getParameter('page', 1));
    $this->game_results->init();        
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->user = $this->getUser()->getGuardUser();
    
    $userid = $this->getUser()->getGuardUser()->getId();
    
    $game_id = $request->getParameter('id');
    $rozgrywka_id = $request->getParameter('q');
    
    $this->game_name = Doctrine_Core::getTable('Game')
      ->createQuery('n')
      ->where('n.id = ?', $game_id)      
      ->execute();
    
    $this->rozgrywka_id = $request->getParameter('q');
    $this->game_id = $request->getParameter('id');
    $this->person_id = $userid;
        
    $query = Doctrine_Query::create()
      ->from('GameResult a')      
      ->where('a.games_id = ?', $game_id)
      ->addWhere('a.user_id = ?', $userid)
      ->addWhere('a.rozgrywka_id = ?', $rozgrywka_id)
      ->addOrderBy('a.end_date DESC');
    
    $this->start_date = myTools::getStartDateByRozgrywkaId($rozgrywka_id, $game_id, $userid);
    
    $this->game_results = new sfDoctrinePager(
        'GameResults',
        sfConfig::get('app_max_games_result_on_page')
    );
    $this->game_results->setQuery($query);
    $this->game_results->setPage($request->getParameter('page', 1));
    $this->game_results->init();
    
  }

  public function executeWynikipacjentlista(sfWebRequest $request)
  {
    $person_id = $this->person_id = $request->getParameter('personid');    
    
    $this->user = $this->getUser()->getGuardUser();
        
    $query = Doctrine_Query::create()
      ->from('GameResult a')
      ->select('a.*, DATEDIFF(NOW(), a.end_date) AS ile_dni, COUNT(a.score) as ilosc, MAX(a.score) as max_pkt, AVG(a.score) as avg_pkt')
      ->where('a.user_id = ?', $person_id)
      ->groupBy('a.games_id')
      ->addOrderBy('a.end_date DESC');    
    
    $this->games_results = new sfDoctrinePager(
        'GameResults',
        sfConfig::get('app_max_games_result_on_page')
    );
    $this->games_results->setQuery($query);
    $this->games_results->setPage($request->getParameter('page', 1));
    $this->games_results->init();    
    
  }  

  public function executeWynikipacjenta(sfWebRequest $request)
  {
    $this->user = $this->getUser()->getGuardUser();
    
    $person_id = $this->person_id = $request->getParameter('personid');    
    $game_id = $this->game_id = $request->getParameter('id'); 
    
    $query = Doctrine_Query::create()
      ->from('GameResult a')
      ->select('a.*, DATEDIFF(NOW(), a.end_date) AS ile_dni, COUNT(a.score) as ilosc, MAX(a.score) as max_pkt, AVG(a.score) as avg_pkt')
      ->where('a.user_id = ?', $person_id)
      ->groupBy('a.games_id')
      ->addGroupBy('a.rozgrywka_id')
      ->addOrderBy('a.end_date DESC');    
    
    $this->game_results = new sfDoctrinePager(
        'GameResults',
        sfConfig::get('app_max_game_result_on_page')
    );
    $this->game_results->setQuery($query);
    $this->game_results->setPage($request->getParameter('page', 1));
    $this->game_results->init();    
  }

  public function executeWynikipacjentaszczegolowe(sfWebRequest $request)
  {
    $this->user = $this->getUser()->getGuardUser();
    
    $person_id = $this->person_id = $request->getParameter('personid');    
    $game_id = $this->game_id = $request->getParameter('idgra'); 
    $rozgrywka_id = $this->rozgrywka_id = $request->getParameter('id');  
    
    $query = Doctrine_Query::create()
      ->from('GameResult a')
      ->select('a.*, DATEDIFF(NOW(), a.end_date) AS ile_dni')
      ->where('a.user_id = ?', $person_id)
      ->addWhere('a.rozgrywka_id = ?', $rozgrywka_id)
      ->addOrderBy('a.end_date DESC');    
    
    $this->game_results = new sfDoctrinePager(
        'GameResults',
        sfConfig::get('app_max_game_result_on_page')
    );
    $this->game_results->setQuery($query);
    $this->game_results->setPage($request->getParameter('page', 1));
    $this->game_results->init();    
  }  
  
  public function executeWykres(sfWebRequest $request)
  {
    $this->setLayout(false);
    $this->user = $this->getUser()->getGuardUser();
    
    $userid = $this->getUser()->getGuardUser()->getId();
    
    if(null == $user_id = $this->user_id = $request->getParameter('personid')):
        $user_id = $userid;
    endif;
    
    $game_id = $this->game_id = $request->getParameter('idgra');
    
    if(null == $rozgrywka_id = $this->rozgrywka_id = $request->getParameter('idrozgrywka') ):
        $query = Doctrine_Query::create()
          ->from('GameResult a')
          ->where('a.games_id = ?', $game_id)
          ->addWhere('a.user_id = ?', $user_id)
          ->groupBy('a.rozgrywka_id')
          ->addOrderBy('a.end_date DESC');
    else:
        $query = Doctrine_Query::create()
          ->from('GameResult a')
          ->where('a.games_id = ?', $game_id)
          ->addWhere('a.user_id = ?', $user_id)
          ->addWhere('a.rozgrywka_id = ?', $rozgrywka_id)
          ->addOrderBy('a.end_date DESC');    
    endif;
    
    $this->game_results = new sfDoctrinePager(
        'GameResults',
        sfConfig::get('app_max_game_result_on_page')
    );
    $this->game_results->setQuery($query);
    $this->game_results->setPage($request->getParameter('page', 1));
    $this->game_results->init(); 

  }

  public function executePrzebieg(sfWebRequest $request)
  {
   
    $person_id = $this->person_id = $request->getParameter('personid'); 
    $this->user = $this->getUser()->getGuardUser();
    
    $userid = $this->getUser()->getGuardUser()->getId();
    
    $result_id = $this->result_id = $request->getParameter('idresult');
    
    $csv_link = Doctrine_Core::getTable('GameResult')
      ->createQuery('a')
      ->where('a.id = ?', $result_id)
      ->limit(1)
      ->fetchOne();
    
    $path = $csv_link->result_xml;
    if (file_exists($path)):
        $uchwyt = fopen ($path, "r");
        while (!feof ($uchwyt)) {
            $buffer = fgets($uchwyt, 4096);
            //echo $buffer;
            $data[] = (int)$buffer;
        }
        $this->data = $data;
        fclose ($uchwyt);
        $this->error = '0';
    else:
        $this->error = '1';
    endif;
    
    
  }  
  
  public function executePrzebiegWykres(sfWebRequest $request)
  {
    
      
    $this->setLayout(false);
    $this->user = $this->getUser()->getGuardUser();
    
    $userid = $this->getUser()->getGuardUser()->getId();
    
    $result_id = $this->result_id = $request->getParameter('idresult');
    
    $this->csv_link = Doctrine_Core::getTable('GameResult')
      ->createQuery('a')
      ->where('a.id = ?', $result_id)
      ->limit(1)
      ->fetchOne(); 

    
  }
  
  public function executeNew(sfWebRequest $request)
  {
    $this->form = new GameResultForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new GameResultForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($game_result = Doctrine_Core::getTable('GameResult')->find(array($request->getParameter('id'))), sprintf('Object game_result does not exist (%s).', $request->getParameter('id')));
    $this->form = new GameResultForm($game_result);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($game_result = Doctrine_Core::getTable('GameResult')->find(array($request->getParameter('id'))), sprintf('Object game_result does not exist (%s).', $request->getParameter('id')));
    $this->form = new GameResultForm($game_result);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($game_result = Doctrine_Core::getTable('GameResult')->find(array($request->getParameter('id'))), sprintf('Object game_result does not exist (%s).', $request->getParameter('id')));
    $game_result->delete();

    $this->redirect('wyniki/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $game_result = $form->save();

      $this->redirect('wyniki/edit?id='.$game_result->getId());
    }
  }
}
